/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.company.testcenter.action;

import com.company.testcenter.entity.TestCenter;
import com.company.testcenter.entity.WorkStation;
import java.io.Serializable;
import java.util.List;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.ScopeType;
import javax.persistence.EntityManager;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;

/**
 *
 * @author chandrikar
 */
@Name("testcenteraction")
@Scope(ScopeType.PAGE)
public class TestCenterAction implements Serializable{

    @DataModel
    List<TestCenter> testCenters;
    @DataModelSelection
    TestCenter selectedTestCenter;
    private TestCenter testCenter;
    private WorkStation workStation;
    @In(value = "em")
    private EntityManager em;

    @Factory
    public void getTestCenters() {
        testCenters = em.createNamedQuery("getAllTestCenters").getResultList();
    }

    @Create
    public void setUp() {
        testCenter = new TestCenter();
        workStation = new WorkStation();
    }

    public TestCenter getTestCenter() {
        return testCenter;
    }

    public void setTestCenter(TestCenter testCenter) {
        this.testCenter = testCenter;
    }

    public WorkStation getWorkStation() {
        return workStation;
    }

    public void setWorkStation(WorkStation workStation) {
        this.workStation = workStation;
    }

    public void addTestCenter() {
        if (null != testCenter) {
            em.persist(testCenter);
            if (null != workStation && null != workStation.getName()) {
                workStation.setTestCenter(testCenter);
                em.persist(workStation);
            }
        }
        getTestCenters();
    }

    public void deleteTestCenter() {
        TestCenter tc = em.find(TestCenter.class, selectedTestCenter.getTestCenterId());
        em.remove(tc);
        getTestCenters();
    }

    public void updateTestCenter() {
        em.merge(selectedTestCenter);
        getTestCenters();
    }

    public void addWorkStation() {
        WorkStation wc = new WorkStation();
        wc.setTestCenter(selectedTestCenter);
        List<WorkStation> wcList = selectedTestCenter.getWorkStations();
        wcList.add(wc);
    }
}
